const path = require('path')
const utils = require('../utils/file_utils')

// 返回数据中间件
module.exports = async (ctx, next) => {
    //http://127.0.0.1:8888/api/seller
    let url = ctx.request.url // /api/seller
    // 获得文件路径E:\others\echars\code_by_myself\koa_server\data\seller.json
    let filepath = path.join(__dirname, '../data', url.replace('/api', '') + '.json' )
    try {
        let ret = await utils.getFileJsonData(filepath)
        ctx.response.body = ret;
    } catch (error) {
        let ret = {
            errorMsg: '文件资源不存在',
            status: 404
        }
        ctx.response.body = JSON.stringify(ret);
    }
    await next()
}